package com.youdu.Activiy;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.youdu.R;
import com.youdu.fragment.home.HomeFragment;
import com.youdu.fragment.home.MessageFragment;
import com.youdu.fragment.home.MineFragment;



/**
 * @Title:HomeActivity
 * @Package:com.youdu.Activiy
 * @Description:首页面的aty
 * @Auther:YJH
 * @Email:yuannunhua@gmail.com
 * @Date:2018/7/2311:00
 */
public class HomeActivity extends BaseActivity implements View.OnClickListener{

    ImageView mHomeView;
    RelativeLayout mHomeLayoutView;
    ImageView mPondView;
    RelativeLayout mPondLayoutView;
    ImageView mMessageView;
    RelativeLayout mMessageLayoutView;
    ImageView mMineView;
    RelativeLayout mMineLayoutView;
    TextView homeTvView;
    TextView pondTvView;
    TextView messageTvView;
    TextView mineTvView;


    private FragmentManager      fm;
    private HomeFragment         mHomeFragment;
    private Fragment             mCommonFragmentOne;
    private MessageFragment      mMessageFragment;
    private MineFragment         mMineFragment;
    private Fragment             mCurrent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_layout);


        initView();//初始化页面中的全部空控件

        //添加要默认显示的fragment页面
        mHomeFragment = new HomeFragment();
        fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content_layout, mHomeFragment);
        ft.commit();
    }


    /**
     * 初始化页面中的全部空控件
     */
    private void initView() {
        mHomeView = (ImageView) findViewById(R.id.home_image_view);
        mPondView = (ImageView) findViewById(R.id.fish_image_view);
        mMessageView = (ImageView) findViewById(R.id.message_image_view);
        mMineView = (ImageView) findViewById(R.id.mine_image_view);

        mHomeLayoutView = (RelativeLayout) findViewById(R.id.home_layout_view);
        mPondLayoutView = (RelativeLayout) findViewById(R.id.pond_layout_view);
        mMessageLayoutView = (RelativeLayout) findViewById(R.id.message_layout_view);
        mMineLayoutView = (RelativeLayout) findViewById(R.id.mine_layout_view);
        mHomeLayoutView.setOnClickListener(this);
        mPondLayoutView.setOnClickListener(this);
        mMessageLayoutView.setOnClickListener(this);
        mMineLayoutView.setOnClickListener(this);

        homeTvView = (TextView) findViewById(R.id.home_tv_view);
        pondTvView = (TextView) findViewById(R.id.pond_tv_view);
        messageTvView = (TextView) findViewById(R.id.message_tv_view);
        mineTvView = (TextView) findViewById(R.id.mine_tv_view);
    }

    public void onClick(View view) {
        FragmentTransaction ft = fm.beginTransaction();
        switch (view.getId()) {
            case R.id.home_layout_view://首页
                mHomeView.setImageResource(R.drawable.ic_home_sel);
                mPondView.setImageResource(R.drawable.ic_pond_no);
                mMessageView.setImageResource(R.drawable.ic_message_no);
                mMineView.setImageResource(R.drawable.ic_mine_no);
                homeTvView.setTextColor(getResources().getColor(R.color.color_fed952));
                pondTvView.setTextColor(getResources().getColor(R.color.color_333333));
                messageTvView.setTextColor(getResources().getColor(R.color.color_333333));
                mineTvView.setTextColor(getResources().getColor(R.color.color_333333));

                hideFragment(mCommonFragmentOne, ft);
                hideFragment(mMessageFragment, ft);
                hideFragment(mMineFragment, ft);
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    ft.add(R.id.content_layout, mHomeFragment);
                } else {
                    mCurrent = mHomeFragment;
                    ft.show(mHomeFragment);
                }
                break;

            case R.id.pond_layout_view://鱼塘
                break;

            case R.id.message_layout_view://消息
                mMessageView.setImageResource(R.drawable.ic_message_sel);
                mHomeView.setImageResource(R.drawable.ic_home_no);
                mPondView.setImageResource(R.drawable.ic_pond_no);
                mMineView.setImageResource(R.drawable.ic_mine_no);
                homeTvView.setTextColor(getResources().getColor(R.color.color_333333));
                pondTvView.setTextColor(getResources().getColor(R.color.color_333333));
                messageTvView.setTextColor(getResources().getColor(R.color.color_fed952));
                mineTvView.setTextColor(getResources().getColor(R.color.color_333333));


                hideFragment(mCommonFragmentOne, ft);
                hideFragment(mHomeFragment, ft);
                hideFragment(mMineFragment, ft);
                if (mMessageFragment == null) {
                    mMessageFragment = new MessageFragment();
                    ft.add(R.id.content_layout, mMessageFragment);
                } else {
                    mCurrent = mMessageFragment;
                    ft.show(mMessageFragment);
                }
                break;

            case R.id.mine_layout_view://我的
                mMineView.setImageResource(R.drawable.ic_mine_sel);
                mHomeView.setImageResource(R.drawable.ic_home_no);
                mPondView.setImageResource(R.drawable.ic_pond_no);
                mMessageView.setImageResource(R.drawable.ic_message_no);
                homeTvView.setTextColor(getResources().getColor(R.color.color_333333));
                pondTvView.setTextColor(getResources().getColor(R.color.color_333333));
                messageTvView.setTextColor(getResources().getColor(R.color.color_333333));
                mineTvView.setTextColor(getResources().getColor(R.color.color_fed952));

                hideFragment(mCommonFragmentOne, ft);
                hideFragment(mMessageFragment, ft);
                hideFragment(mHomeFragment, ft);
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    ft.add(R.id.content_layout, mMineFragment);
                } else {
                    mCurrent = mMineFragment;
                    ft.show(mMineFragment);
                }
                break;
        }

        ft.commit();
    }

    /**
     * 隐藏
     *
     * @param fragment
     * @param ft
     */
    private void hideFragment(Fragment fragment, FragmentTransaction ft) {
        if (fragment != null) {
            ft.hide(fragment);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
